<?php
namespace jy_yxgoodserpfw;
defined('BY_JYA') or exit('error');
/*
	防伪批次
*/
class batch extends \plugin{
	function add_batch($goodsid=0,$beinum=0,$type=0,$remark='',$merchid=0,$xnum=0){
		$setting=$this->plugin_setting('secury');
		if(!$setting['pre'] || !$setting['goods'] ||  !$setting['batch'] || !$setting['serial'] || !$setting['code'] || !$setting['garble']){
			exi('请先设置好-参数设置--防伪码前缀/批号位数/流水号位数/校验码位数/混淆码位数！','error');
		}
		
		$goods=pdo_get('jy_yxgoodserpfw_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodsid));
		if(!$goods){
			exi('指定商品不存在！','error');
		}
		if(!$goods['code']){
			exi('指定商品编码不存在！','error');
		}
		$goods_code=$goods['code'];
		$seckey=random(32);
		$batch_code=rand(pow(10,($setting['batch']-1)), pow(10,$setting['batch'])-1);
		$lastbatch=pdo_get('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'goods_id'=>$goodsid),'','id desc');
		$lastnum=$lastbatch['endnum'];
		$startnum=$lastnum+1;
		$num=$beinum*$xnum;
		$endnum=$startnum+$num-1;
		$lastxnum=$lastbatch['endxn'];
		$startxn=$lastxnum+1;
		$endxn=$startxn+$xnum-1;
		$codedata=array(
			'num'=>$num,
			'startnum'=>$startnum,
			'endnum'=>$endnum,
			'xnum'=>$xnum,
			'startxn'=>$startxn,
			'endxn'=>$endxn,
		);
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'goods_id'=>$goodsid,
			'goods_code'=>$goods_code,
			'seckey'=>$seckey,
			'code'=>$batch_code,
			'status'=>1,
			'type'=>$type,
			'remark'=>$remark,
			'pre'=>$setting['pre'],
			'xpre'=>$setting['xpre'],
			'num'=>$num,
			'beinum'=>$beinum,
			'startnum'=>$startnum,
			'endnum'=>$endnum,
			'xnum'=>$xnum,
			'startxn'=>$startxn,
			'endxn'=>$endxn,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_yxgoodserpfw_batch',$data);
		$batch_id=pdo_insertid();
		$made_security_code=$this->addxcode($data,$batch_id);
		$res=$this->upbatchfile($made_security_code,$batch_id,$batch_code,$type);
		return $res;
	}
	function made_security_code($goods_code='',$batch_code='',$codedata=array(),$seckey=''){
		$setting=$this->plugin_setting('secury');
		$security_codes=array();
		for($i=1;$i<=$codedata['num'];$i++){
			$temp=$setting['pre'].sprintf("%'0{$setting['goods']}s",$goods_code).sprintf("%'0{$setting['batch']}s",$batch_code).sprintf("%'0{$setting['serial']}s",$i);
			$check_code=cfc('tools')->create_check_code($temp,$seckey,$setting['code']);//校验码
			$garble_code=random($setting['garble'],true);//混淆码
			$security_code=$temp.$check_code.$garble_code;
			//$string.=custom_url('jy_yxgoodserpfw/search/main/code='.$security_code,true)."\n";
			$security_codes[]=$security_code;
		}
		$security_xcodes=array();
		for($i=1;$i<=$codedata['xnum'];$i++){
			$temp=$setting['xpre'].sprintf("%'0{$setting['goods']}s",$goods_code).sprintf("%'0{$setting['batch']}s",$batch_code).sprintf("%'0{$setting['serial']}s",$i);
			$check_code=cfc('tools')->create_check_code($temp,$seckey,$setting['code']);//校验码
			$garble_code=random($setting['garble'],true);//混淆码
			$x_codes=$temp.$check_code.$garble_code;
			$security_xcodes[]=$x_codes;
		}
		return array('security_codes'=>$security_codes,'x_codes'=>$security_xcodes);
	}
	function upbatchfile($made_security_code=array(),$batch_id=0,$batch_code=0,$type=1){
		$security_codes=$made_security_code['security_codes'];
		$show_code=$security_codes[0];
		$string='';
		switch($type){
			case 1:
				$string=implode("\r\n",$security_codes);
				break;
			case 2:
				foreach($security_codes as $code){
					$string.=$this->format($code)."\r\n";
				}
				break;
			case 3:
				foreach($security_codes as $code){
					$string.=custom_url('jy_yxgoodserpfw/search//code='.$code,true)."\r\n";
				}
				break;
				
		}
		if(!$string){
			pdo_delete('jy_yxgoodserpfw_batch',array('id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			pdo_delete('jy_yxgoodserpfw_xcode',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			pdo_delete('jy_yxgoodserpfw_code',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			exi('小标签生成失败！','error');
		}
		$total=count($security_codes);
		$filename='oss/'.$_SESSION['uniacid'].'/jy_yxgoodserpfw/'.random(15);
		cfc('files')->mkdirs(ROOT_AT.$filename);
		file_put_contents(ROOT_AT.$filename.'/批号'.$batch_code.'盒码'.$total.'个.txt',$string);
		$he_sn=$made_security_code['he_sn'];
		$list=array();
		foreach($security_codes as $k=>$code){
			if($type==2){
				$code=$this->format($code);
			}elseif($type==3){
				$code=custom_url('jy_yxgoodserpfw/search//xcode='.$code,true);
			}
			$list[$k]['code']=$code;
		}
		foreach($he_sn as $k=>$sn){
			$list[$k]['sn']=$sn;
		}
		$header=array(
			'code'=>'防伪码',
			'sn'=>'流水号',
		);
		model('core/tools')->export2excel($header,$list,$batch_code.'FWcode','',$filename);
		$zip1=cfc('files')->zip(ROOT_AT.$filename,'source');
		$x_codes=$made_security_code['x_codes'];
		$show_xcode=$x_codes[0];
		$string='';
		switch($type){
			case 1:
				$string=implode("\r\n",$x_codes);
				break;
			case 2:
				foreach($x_codes as $code){
					$string.=$this->format($code)."\r\n";
				}
				break;
			case 3:
				foreach($x_codes as $code){
					$string.=custom_url('jy_yxgoodserpfw/search//xcode='.$code,true)."\r\n";
				}
				break;
				
		}
		if(!$string){
			pdo_delete('jy_yxgoodserpfw_batch',array('id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			pdo_delete('jy_yxgoodserpfw_xcode',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			pdo_delete('jy_yxgoodserpfw_code',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
			exi('箱码生成失败！','error');
		}
		$total=count($x_codes);
		$filename='oss/'.$_SESSION['uniacid'].'/jy_yxgoodserpfw/'.random(15);
		cfc('files')->mkdirs(ROOT_AT.$filename);
		file_put_contents(ROOT_AT.$filename.'/批号'.$batch_code.'箱码'.$total.'个.txt',$string);
		$x_sn=$made_security_code['x_sn'];
		$list=array();
		foreach($x_codes as $k=>$code){
			if($type==2){
				$code=$this->format($code);
			}elseif($type==3){
				$code=custom_url('jy_yxgoodserpfw/search//xcode='.$code,true);
			}
			$list[$k]['code']=$code;
		}
		foreach($x_sn as $k=>$sn){
			$list[$k]['sn']=$sn;
		}
		$header=array(
			'code'=>'防伪码',
			'sn'=>'流水号',
		);
		model('core/tools')->export2excel($header,$list,$batch_code.'XWcode','',$filename);
		$xzip=cfc('files')->zip(ROOT_AT.$filename,'source');
		pdo_update('jy_yxgoodserpfw_batch',array('code_file'=>$zip1,'show_code'=>$show_code,'xcode_file'=>$xzip,'show_xcode'=>$show_xcode,),array('id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']));
		return $zip1;
	}
	function addcode($codedata=array(),$batch_id=0,$xiang_id=0,$startnum2=0,$endnum2=0,$setting=array(),$ispage=1){
		$setting=$setting?:$this->plugin_setting('secury');
		$security_codes=array();
		$allnum=$endnum2-$startnum2+1;
		$upnum=$allnum;
		if($allnum>5000){
			$upnum=5000;
		}
		for($i=1;$i<=$upnum;$i++){
			$temp=$setting['pre'].sprintf("%'0{$setting['goods']}s",$codedata['goods_code']).sprintf("%'0{$setting['batch']}s",$codedata['code']).sprintf("%'0{$setting['serial']}s",$i);
			$isdo=0;
			do{
				$check_code=cfc('tools')->create_check_code($temp,$codedata['seckey'],$setting['code']);//校验码
				$garble_code=random($setting['garble'],true);//混淆码
				$security_code=$temp.$check_code.$garble_code;
				/*$isdo++;
				if($isdo>20){
					exi('同批次生成的码太多了！可添加混淆码位数','error');
				}
				$check=pdo_getcolumn('jy_yxgoodserpfw_code',array('uniacid'=>$_SESSION['uniacid'],'code'=>$security_code),'id');*/
			}while($check);
			if(!$check){
				//$string.=custom_url('jy_yxgoodserpfw/search/main/code='.$security_code,true)."\n";
				$security_codes[]=$security_code;
				$k=$i-1;
				$sn=$startnum2+$k;
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'sn'=>$sn,
					'code'=>$security_code,
					'goods_id'=>$codedata['goods_id'],
					'batch_id'=>$batch_id,
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_yxgoodserpfw_code',$data);
			}
		}
		if($allnum>5000){
			$startnum=$startnum2+5000;
			exi('生成下一批5000个盒码！','','/batch/piciaddcode/batch_id='.$batch_id.'&startnum='.$startnum.'&endnum='.$endnum2);
		}
		if($ispage){
			$list=pdo_getall('jy_yxgoodserpfw_xcode',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']),array('code','sn'));
			$pre=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$batch_id),'xpre');
			$security_xcodes=array();
			$x_sn=array();
			foreach($list as $item){
				$security_xcodes[]=$item['code'];
				$item['sn']=$pre.sprintf("%08d", $item['sn']);
				$x_sn[]=$item['sn'];
			}
			$list=pdo_getall('jy_yxgoodserpfw_code',array('batch_id'=>$batch_id,'uniacid'=>$_SESSION['uniacid']),array('code','sn'));
			$pre=pdo_getcolumn('jy_yxgoodserpfw_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>$batch_id),'pre');
			$security_codes=array();
			$he_sn=array();
			foreach($list as $item){
				$security_codes[]=$item['code'];
				$item['sn']=$pre.sprintf("%08d", $item['sn']);
				$he_sn[]=$item['sn'];
			}
			$made_security_code=array('x_sn'=>$x_sn,'he_sn'=>$he_sn,'security_codes'=>$security_codes,'x_codes'=>$security_xcodes);
			$res=$this->upbatchfile($made_security_code,$batch_id,$codedata['code'],$codedata['type']);
			exi('操作完成！','','/batch/');
		}
		return $security_codes;
	}
	function addxcode($codedata=array(),$batch_id=0){
		$setting=$this->plugin_setting('secury');
		$security_xcodes=array();
		for($i=1;$i<=$codedata['xnum'];$i++){
			$temp=$setting['xpre'].sprintf("%'0{$setting['goods']}s",$codedata['goods_code']).sprintf("%'0{$setting['batch']}s",$codedata['code']).sprintf("%'0{$setting['serial']}s",$i);
			$isdo=0;
			do{
				$check_code=cfc('tools')->create_check_code($temp,$codedata['seckey'],$setting['code']);//校验码
				$garble_code=random($setting['garble'],true);//混淆码
				$x_code=$temp.$check_code.$garble_code;
				//$isdo++;
				//if($isdo>20){
				//	exi('同批次生成的码太多了！可添加混淆码位数','error');
				//}
				//$check=pdo_getcolumn('jy_yxgoodserpfw_xcode',array('uniacid'=>$_SESSION['uniacid'],'code'=>$x_code),'id');
			}while($check);
			if(!$check){
				$security_xcodes[]=$x_code;
				$k=$i-1;
				$sn=$codedata['startxn']+$k;
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'sn'=>$sn,
					'code'=>$x_code,
					'goods_id'=>$codedata['goods_id'],
					'batch_id'=>$batch_id,
					'num'=>$codedata['beinum'],
					'createtime'=>TIMESTAMP,
				);
				pdo_insert('jy_yxgoodserpfw_xcode',$data);
			}
		}
		$startnum2=$codedata['startnum'];
		$endnum2=$startnum2+$codedata['xnum']*$codedata['beinum']-1;
		$security_codes=$this->addcode($codedata,$batch_id,$xiang_id,$startnum2,$endnum2,$setting);
		return array('security_codes'=>$security_codes,'x_codes'=>$security_xcodes);
	}
}